<template>
  <panel title="修改密码">
    <el-form label-width="100px" :model="pwd" :rules="rules" ref="pwd">
      <el-form-item label="原密码" prop="oldPwd">
        <el-input type="password" placeholder="请输入原密码" v-model="pwd.oldPwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input type="text" placeholder="请输入新密码" v-model="pwd.newPwd"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="newPwd2" class="remove">
        <el-input type="text" placeholder="请输入新密码" v-model="pwd.newPwd2"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="changePwd">确认</el-button>
        <el-button @click="resetForm('pwd')">重置</el-button>
      </el-form-item>
    </el-form>
  </panel>
</template>

<script>
import axios from "@/utils/requests";
import { accNewPwd, accRevise } from '@/api/account.api';
import { validateRevise, validateRevise2 } from "@/utils/validtate"
export default {
  data() {
    // const
    // const validateRevise = (rule, value, callback) => {
    //   if (!value) {
    //     return callback(new Error("请输入密码"));
    //   }
    //   accRevise(value).then(res => {
    //     if (res.data.code === 0) callback()
    //     else callback(new Error('旧密码错误'))
    //   })
    // };
    // console.log(this);

    return {
      pwd: {
        oldPwd: "",
        newPwd: "",
        newPwd2: "",
      },
      rules: {
        oldPwd: [
          // { required: true, message: "请输入原密码", trigger: "blur" },
          { required: true, validator: validateRevise, trigger: 'blur' },

        ],
        newPwd: [
          // { required: true, message: "请输入新密码", trigger: "change" },
          // { required: true, validator: validateRevise, trigger: "change" },
          { required: true, validator: validateRevise2.bind(this), trigger: "change" },

        ],
        newPwd2: [
          // { required: true, message: "请确认密码", trigger: "change" },
          { required: true, validator: validateRevise2.bind(this), trigger: "change" },
        ],
      },
    };
  },
  methods: {
    changePwd() {
      this.$refs.pwd.validate(async (res) => {
        console.log(this.pwd.newPwd);
        if (res) {
          const res2 = await accNewPwd(this.pwd)
          console.log(res2);
          if (res2.data.code === 0) {
            this.$message.success(res2.data.msg)
            setTimeout(() => {
              sessionStorage.clear()
              this.$router.push('/login')
            }, 2000);
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.remove {
  /deep/ .el-form-item__label:before {
    display: none;
  }
}
</style>